from django.http import HttpResponse,HttpRequest
from dss.Serializer import serializer

def response_as_json(data,foreign_penetrate=False):
    jsonString = serializer(data = data,output_type="json",foreign=foreign_penetrate)
    response = HttpResponse(
        jsonString,
        content_type="application/json",
    )
    response["Access-Control-Allow-Origin"] = "*"
    return response


def json_response(data,code=200,foreign_penetrate=False,**kwargs):
    data = {
        "code":code,
        "msg":"成功",
        "data":data
    }

    return response_as_json(data,foreign_penetrate=foreign_penetrate)


def json_error(error_string="",code=500,**kwargs):
    data = {
        "code":code,
        "msg":error_string,
        "data":{}
    }
    data.update(kwargs)
    return response_as_json(data)

JsonResponse = json_response
JsonError = json_error

